import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class BlockingQueueTest {//阻塞队列,实现生产者消费者模型
    public static void main(String[] args) {
        BlockingQueue<Integer> blockingQueue=new LinkedBlockingQueue<>();//阻塞队列
        //消费者线程
        Thread t1=new Thread(()->{
            while (true) {
                try {
                        int value=blockingQueue.take();
                        System.out.println("消费者:"+value);
                    }
                 catch (InterruptedException e){
                throw new RuntimeException(e);
                }
            }
        });
        //生产者线程
        Thread t2=new Thread(()->{
            int value=0;
            while (true){
                System.out.println("生产者"+value);
                try {
                    blockingQueue.put(value);
                    value++;
                   Thread.sleep(1);//隔1秒生产一次,更加清晰
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();
    }
}
